;(function($){
	$.fn.spa_return_dish = function(opts){
		var el = this;
		var opt = {
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.initHtml();
		this.getReturnDishInfo();
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var $t=this;
    		var $opt=param;
    		$t.$element = el;

			//输入钱数
    		el.find("#pay_btn").bind("click",function(){
    			//计算器
				var status = false;
				el.find("#modal-dialog").hide();
				if(!$.isFunction($().wechatAlipayPay)){
					$.ajaxSetup({ async: false, cache: false });
		            $.getScript("js/openSpa/jquery.plugin.digitalCalculator.js");
		            $.ajaxSetup({ async: true });
				}
				el.find("#mykeyboard").digitalCalculator({
					number:el.find("#pay").val()?el.find("#pay").val():1,//数量
					status:status?status:false,
					title:param.title?param.title:"",
					//初始化数据返回回调
					dataBack:function(number){
						el.find("#modal-dialog").show();
						el.find("#mykeyboard").hide();
 						el.find("#pay").val(number);
 						if(number > parseInt(param.price)&&param.totalPrice!=number){
 							el.find("#change").val(checkTwoPoint(number * 1 - param.price * 1));
 						}
					},
					returnBack :function(){
						el.find("#modal-dialog").show();
						el.find("#mykeyboard").hide();
					}
				});
    		});
    		
    		//选择备注
    		el.find(".orderTaste").on("click","button",function(){
    			var remark = el.find("#remark").val();
    			if($(this).hasClass("btn-primary")){
    				$(this).removeClass("btn-primary").addClass("btn-light");
    				remark = remark.replace(""+ $(this).text() +",","");
    			}else{
    				$(this).removeClass("btn-light").addClass("btn-primary");
					remark = remark + $(this).text() + ",";    						
    			}
    			el.find("#remark").val(remark);
    		});
    		
    		//后厨出单选择
    		el.find("#billing").bind("click",function(){
    			if(!$(this).prop("disabled")){
    				if($(this).is(':checked')){
    					$(this).attr("data-print",0);
    				}else{
    					$(this).attr("data-print",1);
    				}
    			}
    		});
    		
    	
    		//返回
    		el.find("#modalBtnCancel").bind("click",function(){
    			$opt.returnBack();
    		});
    		
    		//确定
    		el.find("#modalBtnSave").bind("click",function(){
    			el.find("#btnSure").attr("disabled","disabled");
    			if(!$.isFunction($().posorderRetreat)){
					$.ajaxSetup({ async: false, cache: false });
		            $.getScript("js/cashier_mana/posorderRetreat.js");
		            $.ajaxSetup({ async: true });
				}
    			$("").posorderRetreat({
					no:$opt.no?$opt.no:"",//no
					xh:$opt.list.xh?$opt.list.xh:"",//序号
					reason:el.find("#remark").val()?el.find("#remark").val():"",//退还原因
					sl:el.find("#pay").val()?el.find("#pay").val():"",//数量
					printchecked:el.find("#billing").attr("data-print")?el.find("#billing").attr("data-print"):"0",//出单
					//初始化数据返回回调
					getDefaultBack:function(data){
//						console.log(data);
						el.find("#btnSure").removeAttr("disabled");
						$opt.getDefaultBack();
					},
					returnBack : function(){
						el.find("#btnSure").removeAttr("disabled");
					}
				});	
    		});
			
		},
		initHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			this.el.addClass("modal").attr("tabindex","-2").attr("data-backdrop","static");
			var html = 	'<div class="mykb-box" id="mykeyboard"></div>'+
						'<div id="modal-dialog" class="modal-dialog" style="width:600px;">'+
							'<div id="pageOverlay" class="hide"></div>'+
							'<div id="loading" class="loading hide">'+
								'<i class="ace-icon fa fa-spinner fa-spin orange bigger-250"></i>'+
							'</div>'+
							'<div class="modal-content">'+
								'<div class="modal-header no-padding">'+
									'<div class="table-header">'+
										'<button id="close-dialog" type="button" class="close" data-dismiss="modal" aria-hidden="true" value="HTML">'+
											'<span class="white">&times;</span>'+
										'</button>'+
										'<span id="title-dialog">'+$.i18n.propJsp(param.title)+'</span>'+
									'</div>'+
								'</div>'+
								'<div class="modal-body" style="min-height: 100px;">'+
									
									'<div class="widget-box row">'+
										'<div class="widget-body">'+  
											'<div id="id_profile">'+
												'<div class="col-sm-12">'+
													'<div class="profile-user-info">'+
			    										'<div class="profile-info-row">'+
			    											'<div id="label_rate" class="profile-info-name necessary-filling">'+$.i18n.propJsp("菜品名称")+'</div>'+
			    											'<div class="profile-info-value">'+
			    												'<span id="rname" class="" data-type="text" title=""></span>'+
			    											'</div>'+
			    										'</div>'+
			    										'<div class="profile-info-row">'+
			    											'<div class="profile-info-name">'+$.i18n.propJsp("可退数量")+'</div>'+
			    											'<div class="profile-info-value">'+
			    												'<span id="rnumber" class="" data-type="text" title=""></span>'+
			    											'</div>'+
			    										'</div>'+
			    									'</div>'+
			    								'</div>'+
					    					'</div>'+  
										'</div>'+  
									'</div>'+ 
									
									'<form id="frm" class="form-horizontal">'+
										'<div class="space-6"></div>'+
										'<div class="form-group">'+
											'<label class="col-sm-3 control-label">'+$.i18n.propJsp("数量:")+'</label>'+
											'<div class="col-sm-9">'+
												'<div class="input-group" style="width:245px;">'+
													'<input type="text" class="form-control search-query input_all" placeholder="" name="disrate" id="pay" value="">'+
													'<span class="input-group-btn">'+
														'<button type="button" class="btn btn-primary btn-sm" id="pay_btn">'+
															'<span class="ace-icon fa fa-keyboard-o icon-on-right bigger-110"></span>'+
														'</button>'+
													'</span>'+
												'</div>'+
											'</div>'+
										'</div>'+
										'<div class="space-6"></div>'+
										'<div class="form-group">'+
											'<label class="col-sm-3 control-label">'+$.i18n.propJsp("备注:")+'</label>'+
											'<div class="col-sm-9">'+
												'<span class="input-icon input-icon-right">'+
													'<textarea class="form-control search-query" cols="50" rows="5" placeholder="" name="disrate" id="remark"></textarea>'+
												'</span>'+
											'</div>'+
										'</div>'+
									'</form>'+
									'<div class="widget-box modal-list transparent" style="height:120px;">'+  
										'<div class="orderTaste"></div>'+
									'</div>'+
								'</div>'+
								'<div class="modal-footer">'+
									'<div class="clearfix">'+
										'<div class="pull-left">'+
											'<label class="inline">'+
												'<input type="checkbox" class="ace" id="billing" data-print="0">'+
												'<span class="lbl"> '+$.i18n.propJsp("后厨出单")+'</span>'+
											'</label>'+
										'</div>'+
										'<div class="pull-right">'+
											'<div class="btn-group">'+
												'<button id="modalBtnSave" type="button" class="btn btn-sm btn-info">'+
													'<i class="ace-icon fa fa-check"></i>'+
													$.i18n.propJsp("确定")+
												'</button>'+
											'</div>'+
											'<div class="btn-group">'+
												'<button id="modalBtnCancel" type="button" class="btn btn-sm btn-default">'+
													'<i class="ace-icon fa fa-undo"></i>'+
													$.i18n.propJsp("返回")+
												'</button>'+
											'</div>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>'+
						'</div>';
			el.html(html);
		},
		getReturnDishInfo : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//退菜信息
    		if(!$.isFunction($().posorderRetreatDish)){
				$.ajaxSetup({ async: false, cache: false });
	            $.getScript("js/cashier_mana/posorderRetreatDish.js");
	            $.ajaxSetup({ async: true });
			}
    		$("").posorderRetreatDish({
    			no:param.no?param.no:"",//no
    			xh:param.list.xh?param.list.xh:"",//序号
    			getDefaultBack:function(data){
    				param.reason = data.ct_message;
    				t.dishData(data);
    				t.bindEvent();
    			}
    		});
		},
		dishData : function(data){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var $t=t;
			$t.$element = el;
    		var $opt=param;
			
    		$t.$element.find("#rname").html(data.ct_brxsxm[0].mc +"&emsp;"+ data.ct_brxsxm[0].bh +"&emsp;"+data.ct_brxsxm[0].dj).attr("title",data.ct_brxsxm[0].mc);
    		$t.$element.find("#rnumber").text(data.ct_brxsxm[0].sl).attr("title",data.ct_brxsxm[0].sl);
    		if(parseFloat($opt.list.sl) < parseFloat(data.ct_brxsxm[0].sl)){
    			$t.$element.find("#pay").val($opt.list.sl);
    		}else{
    			$t.$element.find("#pay").val(data.ct_brxsxm[0].sl);
    		}
    		if(data.chk_printchecked == 1){
    			$t.$element.find("#billing").prop("checked",true).attr("data-print",1);
    		}
    		if(!data.chk_printenabled || data.chk_printenabled == 0){
    			$t.$element.find("#billing").prop("disabled",true);
    		}
    		
    		if(data.ct_message.length > 0){
    			if(Math.ceil(data.length / 4) > 5){
    				$t.$element.find(".modal-list .orderTaste").css({"width":513});
    			}else{
    				$t.$element.find(".modal-list .orderTaste").css({"width":573});
    			}
//    			console.log(JSON.stringify(data.ct_message))
				$(data.ct_message).each(function(key,value){
					var btn_html = $('<button type="button" class="btn btn-app btn-light" style="padding:8px;">'+ value.mc +'</button>');
    				if(value.mc.length > Math.floor((($t.$element.find(".orderTaste").width() - 18) / 4) / 18)){
    					btn_html.css("font-size",(($t.$element.find(".orderTaste").width() - 18) / 4) / value.mc.length);
        			}
    				$t.$element.find(".modal-list .orderTaste").append(btn_html);
	    		});
				$t.$element.find(".modal-list button").css({"width":($t.$element.find(".orderTaste").width() - 18) / 4,"height":42.5});
    		}
		},
	}
	
	function checkTwoPoint(value){  		
	    value=value.toString()+".00";
	    value = value.replace(/\.{2,}/g,"."); //只保留第一个, 清除多余的
	    value = value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
	    value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
	    return value;
	}
	
})(jQuery);